package yunwo.cn.yzstation.view.activity;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.squareup.picasso.Picasso;



import de.hdodenhof.circleimageview.CircleImageView;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.ImainView;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.api.okapi.UserinfoCallBack;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.UserInfoBean;
import yunwo.cn.yzstation.presenter.MainActivityPersenter;
import yunwo.cn.yzstation.printer.NewSetPaintActivity;
import yunwo.cn.yzstation.utils.StringUtil;


public class MainActivity extends MyBaseActivity implements View.OnClickListener,ImainView{

    private RelativeLayout mTest;
    //    private SlidingMenu mMenu;
    private ImageView mImg;
    private ImageView mGerenzhongxin;
    private RelativeLayout mInventoryRL;
    private RelativeLayout mTakegoods;
    private RelativeLayout mAssembly;
    private RelativeLayout mAssembly1;
    private Intent mIntent;
    private SlidingMenu mMenu;

    private LinearLayout mStatistics;
    private LinearLayout mSetting;
    private LinearLayout mSchedulingcar;
    private LinearLayout mQueryorder;
    private RelativeLayout mOpenorder;
    private MainActivityPersenter mMainActivityPersenter;
    private CircleImageView mPic;
    private TextView mUsername;
    private TextView mPhone;
    private LinearLayout mPrinter;
    private static final int CALL_PHONE_INCODE = 10;
    private LinearLayout mProduct;
    private RelativeLayout mInboundfreight;
    private Intent mIntent1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initview();
        initSlidingMenu();
        initSlidingMEnuView();
        initEvent();
        setNativeData();
        String authorization = Yzapp.getInstance().getAuthorization();
        Log.d("authorization", authorization);
        //获取所有数据字典
        mMainActivityPersenter.getDictionariesData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mMainActivityPersenter.getUserInfo();
    }

    private void initEvent() {

        //获取用户信息
        mMainActivityPersenter.getUserInfo();

    }

    private void setNativeData() {
        //初始化用户信息
        mUsername.setText(Yzapp.getInstance().getUserName());
        mPhone.setText(Yzapp.getInstance().getmobile());
        mMainActivityPersenter.setPersonIcon();
    }



    private void initSlidingMEnuView() {
        mProduct = (LinearLayout) mMenu.findViewById(R.id.Product);
        mPhone = (TextView) mMenu.findViewById(R.id.phone);
        mPic = (CircleImageView) mMenu.findViewById(R.id.pic);
        mUsername = (TextView) mMenu.findViewById(R.id.username);
        mPrinter = (LinearLayout) mMenu.findViewById(R.id.printer);
        mSetting = (LinearLayout) mMenu.findViewById(R.id.setting);
        mStatistics = (LinearLayout) mMenu.findViewById(R.id.Statistics);
        mQueryorder = (LinearLayout) mMenu.findViewById(R.id.queryorder);
        mSchedulingcar = (LinearLayout) mMenu.findViewById(R.id.schedulingcar);
                mProduct.setOnClickListener(this);
        mPrinter.setOnClickListener(this);
        mSetting.setOnClickListener(this);
        mQueryorder.setOnClickListener(this);
        mStatistics.setOnClickListener(this);
        mSchedulingcar.setOnClickListener(this);
        mMainActivityPersenter = new MainActivityPersenter(this, this);
    }

    @Override
    protected String settiltile() {
        return "云货宝";
    }

    private void initview() {

        mImg = (ImageView) findViewById(R.id.img);
        mAssembly = (RelativeLayout) findViewById(R.id.assembly);
        mAssembly1 = (RelativeLayout) findViewById(R.id.assembly1);
        mTakegoods = (RelativeLayout) findViewById(R.id.takegoods);
        mGerenzhongxin = (ImageView) findViewById(R.id.gerenzhongxin);
        mInventoryRL = (RelativeLayout) findViewById(R.id.InventoryRL);
        mInboundfreight = (RelativeLayout) findViewById(R.id.inboundfreight);
        mImg.setOnClickListener(this);
        mAssembly.setOnClickListener(this);
        mAssembly1.setOnClickListener(this);
        mTakegoods.setOnClickListener(this);
        mInventoryRL.setOnClickListener(this);
        mInboundfreight.setOnClickListener(this);

        mGerenzhongxin.setOnClickListener(this);
        AnimationDrawable animationDrawable = (AnimationDrawable) mImg.getDrawable();
        animationDrawable.start();


    }


    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_main;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.inboundfreight:
                mIntent = new Intent(MainActivity.this, TrainnumberActivity.class);
                startActivity(mIntent);
                break;
            case R.id.Product:
                mIntent =  new Intent(MainActivity.this, ProductActivity.class);
                startActivity(mIntent);

                break;
            case R.id.gerenzhongxin:
                mMenu.toggle();
                break;
            case R.id.img:
                mIntent = new Intent(MainActivity.this, OrderActivity.class);
                startActivity(mIntent);
                break;
            case R.id.InventoryRL:
                mIntent = new Intent(MainActivity.this, InventoryActivity.class);
                startActivity(mIntent);
                break;
            case R.id.takegoods:
                mIntent = new Intent(MainActivity.this, TakegoodsActivity.class);
                startActivity(mIntent);
                break;
            case R.id.Statistics:
                mIntent = new Intent(MainActivity.this, StatisticsActivity.class);
                startActivity(mIntent);
                break;
            case R.id.setting:
                mIntent = new Intent(MainActivity.this, SettingActivity.class);
                startActivityForResult(mIntent, 99);
                break;
            case R.id.schedulingcar:
                mIntent = new Intent(MainActivity.this, SchedulingActivity.class);
                startActivity(mIntent);
                break;
            case R.id.queryorder:
                mIntent = new Intent(MainActivity.this, QueryorderActivity.class);
                startActivity(mIntent);
                break;
            case R.id.assembly:
                mIntent = new Intent(MainActivity.this, AssembleActivity.class);
                mIntent.putExtra("main", "1");
                startActivity(mIntent);
                break;
            case R.id.assembly1:
                mIntent = new Intent(MainActivity.this, AssembleActivity.class);
                mIntent.putExtra("main", "2");
                startActivity(mIntent);
                break;
            case R.id.printer:
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请CAMERA权限
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                            CALL_PHONE_INCODE);
                }
                else {
                    mIntent = new Intent(MainActivity.this, NewSetPaintActivity.class);
                    startActivity(mIntent);
                }
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 98:
                setNativeData();

                break;

        }

    }

    private void initSlidingMenu() {
        mMenu = new SlidingMenu(MainActivity.this);
        mMenu.setMode(com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        mMenu.setTouchModeAbove(com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.TOUCHMODE_FULLSCREEN);
        /*mMenu.setShadowWidthRes(R.dimen.BehindOffsetRes);
        mMenu.setShadowDrawable(R.drawable.blcak_bg);*/
        mMenu.setOffsetFadeDegree(0.4f);

        // 设置滑动菜单视图的宽度
        mMenu.setBehindOffsetRes(R.dimen.BehindOffsetRes);
        // 设置渐入渐出效果的值
        mMenu.setFadeDegree(0.35f);
        /**
         * SLIDING_WINDOW will include the Title/ActionBar in the content
         * section of the SlidingMenu, while SLIDING_CONTENT does not.
         */
        //把滑动菜单添加进所有的Activity中，可选值SLIDING_CONTENT ， SLIDING_WINDOW
        mMenu.attachToActivity(MainActivity.this, com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        mMenu.setMenu(R.layout.layout_menu);
        mMenu.setOnOpenedListener(new com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
//                mPresenter.loadNaviData();
            }
        });
    }


    /*****************
     * 双击退出程序
     ************************************************/
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (mMenu.isMenuShowing()) {
                mMenu.showContent();
                return true;
            }
            // 判断是否在两秒之内连续点击返回键，是则退出，否则不退出
            else  if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                // 将系统当前的时间赋值给exitTime
                exitTime = System.currentTimeMillis();
            } else {
                finishAll();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void showIconImage(String path) {
        Log.d("showIconImage", "showIconImage: path:" + path);

        if (!StringUtil.isEmpty(path)) {
            Picasso.with(MainActivity.this).load(path).into(mPic);
        }
    }

    @Override
    public void requestUserInfoSuccess() {
        setNativeData();
    }


}
